package com.wst.wst_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.wst_server.entity.Money;
import com.wst.wst_server.entity.Org;
import com.wst.wst_server.mapper.MoneyMapper;
import com.wst.wst_server.service.IMoneyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * �������� 服务实现类
 * </p>
 *
 * @author zyk
 * @since 2023-03-24
 */
@Service
public class MoneyServiceImpl extends ServiceImpl<MoneyMapper, Money> implements IMoneyService {

    @Autowired
    MoneyMapper moneyMapper;

    @Override
    public IPage<Money> getbycondition(Map<String, Object> map) {
        QueryWrapper<Money> queryWrapper = new QueryWrapper<Money>().orderByDesc("id");
        Set<String> keys = map.keySet();
        for (String key : keys){
            if("page".equals(key) || "count".equals(key)){
                continue;
            }else {
                boolean condition = map.get(key) != null && StringUtils.hasLength(map.get(key).toString());
                if("orgName".equals(key) || "orgCode".equals(key)){
                    queryWrapper.like(key,map.get(key));
                }else {
                    queryWrapper.eq(key,map.get(key));
                }
            }
        }
        Integer page = Integer.parseInt(map.get("page").toString());
        Integer count = Integer.parseInt(map.get("count").toString());
        IPage<Money> p = new Page<>(page,count);
        return moneyMapper.selectPage(p,queryWrapper);
    }

    @Override
    public List<Money> getyearlyandorgid(Map<String, Object> map) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(map.containsKey("yearly"))
            queryWrapper.eq("yearly",map.get("yearly"));
        if(map.get("orgId[0]")==null){
        }else {
            if(!(Integer.parseInt(map.get("orgId[0]").toString())==0 && map.size() == 2)){
                List<Integer> ids =new ArrayList<>();
                Set<String> keys = map.keySet();
                for (String key : keys){
                    if(key.equals("yearly")){
                        continue;
                    }else {
                        ids.add(Integer.parseInt(map.get(key).toString()));
                    }
                }
                queryWrapper.in("orgid",ids);
            }
        }

        return moneyMapper.getbyyearlyandorgid(queryWrapper);
    }

}
